import * as THREE from "three";
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 导入动画库
import gsap from "gsap";
// 导入dat.gui
import * as dat from "dat.gui";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";

const scene = new THREE.Scene();

// 2、创建相机
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);

// 设置相机位置
camera.position.set(0, 0, 10);
scene.add(camera);

// 设置cube纹理加载器
const cubeTextureLoader = new THREE.CubeTextureLoader();
const envMapTexture = cubeTextureLoader.load([
  "textures/environmentMaps/1/px.jpg",
  "textures/environmentMaps/1/nx.jpg",
  "textures/environmentMaps/1/py.jpg",
  "textures/environmentMaps/1/ny.jpg",
  "textures/environmentMaps/1/pz.jpg",
  "textures/environmentMaps/1/nz.jpg",
]);

// 添加物体
// 创建几何体
const cubeGeometry = new THREE.SphereBufferGeometry(1, 20, 20);
const cubeMaterial = new THREE.MeshStandardMaterial({
  metalness: 0.9,
  roughness: 0.01,
  //   envMap: envMapTexture,
});
// 根据几何体和材质创建物体
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);

// 将几何体添加到场景中
scene.add(cube);
// 给场景添加背景
scene.background = envMapTexture;
// 给场景所有的物体添加默认的环境贴图
scene.environment = envMapTexture;

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 设置控制器阻尼，让控制器更有真实效果,必须在动画循环里调用.update()。
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

let hdrI = 2;
let index = 2;
// gui 控制切换场景
const params = {
  fn: () => {
    // 设置cube纹理加载器
    const getPath = (i) => {
      return [
        `textures/environmentMaps/${i}/px.jpg`,
        `textures/environmentMaps/${i}/nx.jpg`,
        `textures/environmentMaps/${i}/py.jpg`,
        `textures/environmentMaps/${i}/ny.jpg`,
        `textures/environmentMaps/${i}/pz.jpg`,
        `textures/environmentMaps/${i}/nz.jpg`,
      ];
    };
    const cubeTextureLoader = new THREE.CubeTextureLoader();
    const envMapTexture = cubeTextureLoader.load(getPath(index));
    // 给场景添加背景
    scene.background = envMapTexture;
    // 给场景所有的物体添加默认的环境贴图
    scene.environment = envMapTexture;
    index = index === 3 ? 0 : index + 1;
    hdrI = 2;
  },
  fnHdr: () => {
    // 加载hdr环境图
    const rgbeLoader = new RGBELoader();
    rgbeLoader.loadAsync(`textures/hdr/00${hdrI}.hdr`).then((texture) => {
      texture.mapping = THREE.EquirectangularReflectionMapping;
      scene.background = texture;
      scene.environment = texture;
      hdrI = hdrI === 5 ? 2 : hdrI + 1;
      index = 0;
    });
  },
};
const gui = new dat.GUI();
var folder = gui.addFolder("场景切换");
// 设置按钮点击触发某个事件
folder.add(params, "fn").name("多图片切换");
folder.add(params, "fnHdr").name("hdr切换");
function render() {
  controls.update();
  renderer.render(scene, camera);
  //   渲染下一帧的时候就会调用render函数
  requestAnimationFrame(render);
}

render();

// 监听画面变化，更新渲染画面
window.addEventListener("resize", () => {
  //   console.log("画面变化了");
  // 更新摄像头
  camera.aspect = window.innerWidth / window.innerHeight;
  //   更新摄像机的投影矩阵
  camera.updateProjectionMatrix();

  //   更新渲染器
  renderer.setSize(window.innerWidth, window.innerHeight);
  //   设置渲染器的像素比
  renderer.setPixelRatio(window.devicePixelRatio);
});
